﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Accounting;
using School.Common;

namespace School.Pages.Teaching
{
    public partial class CourseSchedule : System.Web.UI.Page
    {
        SchoolEntities db = new SchoolEntities();
        string[] days = { "السبت", "الأحد", "الأثنين", "الثلاثاء", "الأربعاء", "الخميس" };
        string[] courses = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1508) == true)
                {
                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataValueField = "ID";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
                        lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void DdlSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            var sub = long.Parse(DdlSubject.SelectedValue);

            DdlTeacher.DataSource = db.Te_Teacher_Subject.Where(x => x.SubjectId == sub).Select(x => new { x.Id, Teacher = x.ORG_Stuff_Teacher.ORG_Stuff.FName + x.ORG_Stuff_Teacher.ORG_Stuff.MName + x.ORG_Stuff_Teacher.ORG_Stuff.LName });
            DdlTeacher.DataTextField = "Teacher";
            DdlTeacher.DataValueField = "Id";
            DdlTeacher.DataBind();
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataSource = Structure.GetClassesByGrade(long.Parse(LstGrades.SelectedValue));
            lstClasses.DataValueField = "Id";
            lstClasses.DataTextField = "Name";
            lstClasses.DataBind();
            lstClasses.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الفصل" });
        }

        protected void lstClasses_SelectedIndexChanged(object sender, EventArgs e)
        {
            long grade = long.Parse(LstGrades.SelectedValue);
            DdlSubject.DataSource = db.TE_Subject.Where(x => x.Grad_Id == grade);
            DdlSubject.DataValueField = "Id";
            DdlSubject.DataTextField = "Subject_Name";
            DdlSubject.DataBind();

            var sub = long.Parse(DdlSubject.SelectedValue);

            DdlTeacher.DataSource = db.Te_Teacher_Subject.Where(x => x.SubjectId == sub).Select(x => new { x.Id, Teacher = x.ORG_Stuff_Teacher.ORG_Stuff.FName + " " + x.ORG_Stuff_Teacher.ORG_Stuff.MName + " " + x.ORG_Stuff_Teacher.ORG_Stuff.LName });
            DdlTeacher.DataTextField = "Teacher";
            DdlTeacher.DataValueField = "Id";
            DdlTeacher.DataBind();

            string html = "<table>";
            html += "  <tr>";
            html += "        <th>اليوم / الحصة</th>";
            foreach (var x in courses)
            {
                html += "       <th>" + x + "</th>";
            }
            html += "  </tr>";
            for (int i = 0; i < days.Count(); i++)
            {
                html += "    <tr>";
                html += "        <th>" + days[i] + "</th>";
                foreach (var x in courses)
                {                    
                    var value = "";
                    var subname = "";

                    try
                    {
                        var cls = int.Parse(lstClasses.SelectedValue);
                        var sess = int.Parse(x);
                        var d = db.TE_Class_Course.Single(y => y.Class == cls && y.Day == i && y.Session ==sess);
                        value = "value='"+d.Te_Teacher_Subject.Id+"'";
                        subname = d.Te_Teacher_Subject.TE_Subject.Subject_Name;
                    }
                    catch (Exception ex)
                    {
                        var exx = ex;
                    }

                    html += "       <td>";
                    html += "<input type='hidden' name='c_" + x + "_" + i + "' " + value + "/>";
                    html += "<a class='edit_course' href='#' rel='c_" + x + "_" + i + "'>تعديل</a>";
                    html += " ( <span class='course'>" + subname + "</span> )";
                    html += "       </td>";
                }
                html += "    </tr>";
            }
            html += "</table>";

            CoursesContainer.InnerHtml = html;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var cls = int.Parse(lstClasses.SelectedValue);
            var oldcourses = db.TE_Class_Course.Where(x => x.Class == cls);
            
            foreach (var o in oldcourses)
            {
                db.DeleteObject(o);                
            }
            db.SaveChanges();

            for (int i = 0; i < 6; i++)
            {
                for (int h = 1; h < 10; h++)
                {
                    long subject = 0;                    

                    if (Request["c_" + h + "_" + i] != null)
                    {

                        if (long.TryParse(Request["c_" + h + "_" + i].ToString(), out subject))
                        {

                            db.AddToTE_Class_Course(new TE_Class_Course()
                            {
                                Session = h,
                                Day = i,
                                Class = cls,
                                Subject = subject
                            });
                        }
                    }
                }
            }

            db.SaveChanges();
            Response.Redirect("CourseSchedule.aspx");
        }
    }
}